/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.order.remote.RemoteCsrUnlockOrderServiceImpl
   Module Description   :

   Date Created      : 2011/1/14
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.order.remote;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.order.entity.CsrUpdateOrderLog;
import com.yaodian100.ec.order.service.OrderService;

/**
 * @author cut7man
 * 
 */
public class RemoteCsrUnlockOrderServiceImpl implements RemoteCsrUnlockOrderService {
	@Resource
	private BaseManager baseMgr;
	@Resource
	private OrderService orderService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.order.remote.RemoteCsrUnlockOrderService#unlockOrder(java.lang.String)
	 */
	public String unlockOrder(Integer batchSize) {
		Calendar currentDate = Calendar.getInstance();
		currentDate.add(Calendar.HOUR_OF_DAY, -1);
		String hql = "from CsrUpdateOrderLog where enable=? and lockDate<=?";
		List<Object> list = new ArrayList<Object>();
		list.add(true);
		list.add(currentDate.getTime());
		Object[] orderLogs = null;
		if (batchSize != null && batchSize.intValue() > 0) {
			orderLogs = baseMgr.queryByHql(hql, list.toArray(), 0, batchSize);
		} else {
			orderLogs = baseMgr.queryByHql(hql, list.toArray());
		}

		for (Object obj : orderLogs) {
			CsrUpdateOrderLog orderLog = (CsrUpdateOrderLog) obj;
			if (orderLog.isEnable()) {
				JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
				factory.setServiceClass(ClientCsrUpdateOrderService.class);
				factory.setAddress(ServerValue.getCsrLockOrderWS());
				ClientCsrUpdateOrderService client = (ClientCsrUpdateOrderService) factory.create();
				String result = "";
				try {
					result = client.doPartialCancelBlock(orderLog.getOrderId());
				} catch (Exception e) {
					e.printStackTrace();
				}
				if ("true".equals(result)) {
					orderService.unlockOrder(orderLog);
				} else {
					orderLog.setEnable(false);
					baseMgr.update(orderLog);
				}
			}

		}
		return "true";
	}
}
